<template>
  <div>
    <a-select
      :mode="multi==true?'multiple':'default'"
      :showSearch="false"
      style="width: 100%"
      placeholder="请选择团队"
      @deselect="deselect"
      v-model="ids"
      :open="open"
      :disabled="disabled || disabledIds"
      @dropdownVisibleChange="selectIds">
      <a-select-option v-for="item in selectedObj" :key="item.id">{{ item.aliasName }}</a-select-option>
    </a-select>
    <select-team-input-modal ref="inputModal" :query-type="queryType" :urlList="this.url.urlList" :multi="multi"
    :finance="finance" @choseObj="choseObj"></select-team-input-modal>
  </div>
</template>

<script>
import SelectTeamInputModal from './modules/SelectTeamInputModal'
import {getAction} from '@/api/manage'

export default {
  name: "SelectTeamInputCanBack",
  components: {SelectTeamInputModal},
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    purchaseCannibalizeDetailList: {
      type: Array,
      default: () => []
    },
    open: {
      type: Boolean,
      required: false,
      default: false
    },
    value: {
      type: String,
      required: false
    },
    tdlx: {
      type: Number,
      required: false
    },
    finance: {
      type: Boolean,
      default: false,
    },
    multi: {
      type: Boolean,
      default: false,
      required: false
    },
    props: {
      type: Object,
      default: null,
    },
    queryType: {
      type: String,
      required: false,
      default: 'children'
    }
  },
  data() {
    return {
      ids: [],
      selectedObj: [],
      deleteIds: [],
      list: [],
      objs: "",
      isPop: false,
      url: {
        queryById: "/api/erp/omc/teammanager/erpOmcTeamManager/queryById",
        queryByIds: "/api/erp/omc/teammanager/erpOmcTeamManager/queryByIds",
        urlList: "/api/erp/omc/teammanager/erpOmcTeamManager/list2?auditStatus=2&tdlx=" + this.tdlx
      },
      disabledIds: false
    }
  },
  mounted() {
    /*if (this.tdlx) {
      debugger
      this.url.urlList = "/omc/teammanager/erpOmcTeamManager/list2?auditStatus=2&tdlx=" + this.tdlx
    }*/
    //debugger;
    this.initObj(this.value);
  },
  watch: {
    value(val) {
      this.initObj(val);
    },
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    initObj(ids) {
      this.disabledIds = true
      this.deleteIds = [];
      this.selectedObj = [];
      if (ids && ids.length > 0) {
        this.ids = ids.split(",");
        let url = '';
        let param = {};
        if (this.ids.length == 1) {
          url = this.url.queryById;
          param = {id: ids};
        } else {
          url = this.url.queryByIds;
          param = {ids: ids};
        }
        getAction(url, param).then((res) => {
          if (res.success) {
            if (!Array.isArray(res.result)) {
              this.selectedObj.push(res.result);
            } else {
              for (let i = 0; i < res.result.length; i++) {
                this.selectedObj.push(res.result[i]);
              }
            }
            this.$refs.inputModal.edit(res.result, this.ids);
            this.disabledIds = false
          }
        });
      } else {
        this.ids = [];
        this.$refs.inputModal.edit(null, null);
        this.disabledIds = false
      }

    },
    resetObj() {
      this.userType = false;
      this.ids = [];
      this.selectedObj = [];
      this.deleteIds = [];
      this.open = false;
      this.$refs.inputModal.edit(null, null);
    },
    selectIds() {
      let that = this;
      console.log("ids ", this.ids, this.purchaseCannibalizeDetailList)
      if (!this.disabled && !!this.ids && this.ids.length != 0 && this.purchaseCannibalizeDetailList && this.purchaseCannibalizeDetailList.length) {
        if(!this.isPop){
          this.isPop = true;
          this.$confirm({
            title: "是否确认",
            content: "是否确认切换团队，此操作将会清空明细表中已填写的所有信息？",
            okText:'确定',
            cancelText:'取消',
            onOk() {
              if (!that.disabled) {
                that.isPop = false;
                that.deleteId();
                that.$refs.inputModal.add(that.selectedObj, that.ids);
              }
            },
            onCancel() {
              that.isPop = false;
              return;
            }
          });
        }
      } else {
        if (!this.disabled) {
          that.isPop = false;
          this.deleteId();
          that.$refs.inputModal.add(this.selectedObj, this.ids);
        }
      }
    },
    deselect(value, option) {
      this.deleteIds.push(value);
      this.deleteId();
      this.choseObj(this.selectedObj);
    },
    deleteId() {
      let that = this;
      this.selectedObj = this.selectedObj.filter(function (e) {
        return that.deleteIds.indexOf(e.id) == -1;
      })
      this.deleteIds = [];
    },
    // 子modal回调
    choseObj: function (list) {
      this.selectedObj = list;
      console.log('list', list)
      this.ids = [];
      for (var i = 0; i < list.length; i++) {
        this.ids.push(list[i].id);
      }
      this.objs = this.ids.join(",");
      this.$emit("change", this.objs);
      this.$emit("back", this.selectedObj);
    },
  }
}
</script>

<style scoped>

</style>